/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package main

import (
	"flag"
	"fmt"

	"gitee.com/WisdomClassroom/cli/action"
	"gitee.com/WisdomClassroom/cli/glb"
	"gitee.com/WisdomClassroom/core"
)

func main() {
	var (
		userType string
		gender   string
	)

	helpFunc := func() {
		flag.Usage()
		fmt.Println("UserType: 'maintainer', 'manager', 'leader', 'teacher'")
		fmt.Println("          'undergraduate', 'graduateStudent', 'doctoralStudent'")
		fmt.Println("UserGender: 'man', 'woman'")
	}

	core.PublicFlag(BuildTag, BuildGo, BuildTime)
	flag.StringVar(&userType, "user-type", "", "user role type")
	flag.IntVar(&glb.AccountID, "user-account", 0, "user account id")
	flag.StringVar(&glb.Name, "user-name", "", "user name")
	flag.StringVar(&gender, "user-gender", "", "user gender")
	flag.StringVar(&glb.ClassUUID, "user-class", "", "user class uuid")
	flag.Parse()

	if core.FlagHelp {
		helpFunc()
		return
	}
	if core.FlagVersion {
		core.PublicVersion()
		return
	}
	if !core.FlagUseSqlite && !core.FlagUsePG {
		helpFunc()
		return
	}

	switch userType {
	case "maintainer":
		glb.UserType = 2
	case "manager":
		glb.UserType = 3
	case "leader":
		glb.UserType = 4
	case "teacher":
		glb.UserType = 5
	case "undergraduate":
		glb.UserType = 6
	case "graduateStudent":
		glb.UserType = 7
	case "doctoralStudent":
		glb.UserType = 8
	default:
		helpFunc()
		return
	}
	switch gender {
	case "man":
		glb.Gender = 1
	case "woman":
		glb.Gender = 2
	default:
		helpFunc()
		return
	}

	glb.Logger = core.NewLogger(core.FlagLogLevel)

	if core.FlagUseSqlite {
	} else if core.FlagUsePG {
	} else {
		flag.Usage()
		return
	}

	if accountID := action.UserCreate(); accountID == 0 {
		glb.Logger.Info("Create User failed")
	} else {
		glb.Logger.Info(fmt.Sprintf("Create Root User successfully: %d", accountID))
	}
}
